﻿Imports SdlDotNet.Graphics
Imports SdlDotNet.Graphics.Sprites
Imports SdlDotNet.Input
Imports System.Drawing

Public Class LaunchMissileMenu
    Implements IMenu

    Private Position As Point

    Private SmallWarheadAvailableSurface As New Surface("Resources\Images\Game\Menu\SmallWarhead_Available.jpg")
    Private SmallWarheadUnavailableSurface As New Surface("Resources\Images\Game\Menu\SmallWarhead_Unavailable.jpg")
    Private SmallWarheadSelectedSurface As New Surface("Resources\Images\Game\Menu\SmallWarhead_Selected.jpg")
    Private MediumWarheadAvailableSurface As New Surface("Resources\Images\Game\Menu\MediumWarhead_Available.jpg")
    Private MediumWarheadUnavailableSurface As New Surface("Resources\Images\Game\Menu\MediumWarhead_Unavailable.jpg")
    Private MediumWarheadSelectedSurface As New Surface("Resources\Images\Game\Menu\MediumWarhead_Selected.jpg")
    Private LargeWarheadAvailableSurface As New Surface("Resources\Images\Game\Menu\LargeWarhead_Available.jpg")
    Private LargeWarheadUnavailableSurface As New Surface("Resources\Images\Game\Menu\LargeWarhead_Unavailable.jpg")
    Private LargeWarheadSelectedSurface As New Surface("Resources\Images\Game\Menu\LargeWarhead_Selected.jpg")
    Private ExtraLargeWarheadAvailableSurface As New Surface("Resources\Images\Game\Menu\ExtraLargeWarhead_Available.jpg")
    Private ExtraLargeWarheadUnavailableSurface As New Surface("Resources\Images\Game\Menu\ExtraLargeWarhead_Unavailable.jpg")
    Private ExtraLargeWarheadSelectedSurface As New Surface("Resources\Images\Game\Menu\ExtraLargeWarhead_Selected.jpg")
    Private CancelAvailableSurface As New Surface("Resources\Images\Game\Menu\Cancel_Available.jpg")
    Private CancelSelectedSurface As New Surface("Resources\Images\Game\Menu\Cancel_Selected.jpg")
    Private BlankSurface As New Surface("Resources\Images\Game\Menu\Blank_5.jpg")

    Private SmallWarheadButton As Sprite
    Private MediumWarheadButton As Sprite
    Private LargeWarheadButton As Sprite
    Private ExtraLargeWarheadButton As Sprite
    Private BlankButton As Sprite
    Private CancelButton As Sprite

    Private CanSelectSmallWarhead As Boolean = False
    Private CanSelectMediumWarhead As Boolean = False
    Private CanSelectLargeWarhead As Boolean = False
    Private CanSelectExtraLargeWarhead As Boolean = False

    Public Event ClickedWarheadButton(ByVal warheadType As MissileAndWarheadTypes)
    Public Event ClickedCancelButton()

    Public Sub New(ByVal position As Point, ByVal player As Player)
        Me.Position = position
        InitializeSprites()
        ShowMenuForPlayer(player)
    End Sub

    Private Sub InitializeSprites()
        SmallWarheadButton = New Sprite(SmallWarheadUnavailableSurface, New Point(Position.X, Position.Y))
        MediumWarheadButton = New Sprite(MediumWarheadUnavailableSurface, New Point(Position.X, Position.Y + 19))
        LargeWarheadButton = New Sprite(LargeWarheadUnavailableSurface, New Point(Position.X, Position.Y + (19 * 2)))
        ExtraLargeWarheadButton = New Sprite(ExtraLargeWarheadUnavailableSurface, New Point(Position.X, Position.Y + (19 * 3)))
        BlankButton = New Sprite(BlankSurface, New Point(Position.X, Position.Y + (19 * 4)))
        CancelButton = New Sprite(CancelAvailableSurface, New Point(Position.X, Position.Y + (19 * 5)))
    End Sub

    Public Sub Draw(ByRef display As Surface) Implements IMenu.Draw
        display.Blit(SmallWarheadButton)
        display.Blit(MediumWarheadButton)
        display.Blit(LargeWarheadButton)
        display.Blit(ExtraLargeWarheadButton)
        display.Blit(BlankButton)
        display.Blit(CancelButton)
    End Sub

    Public Sub HandleMouseButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Implements IMenu.HandleMouseButtonUp
        If (SmallWarheadButton.IntersectsWith(e.Position) And CanSelectSmallWarhead) Then
            RaiseEvent ClickedWarheadButton(MissileAndWarheadTypes.Small)
        ElseIf (MediumWarheadButton.IntersectsWith(e.Position) And CanSelectMediumWarhead) Then
            RaiseEvent ClickedWarheadButton(MissileAndWarheadTypes.Medium)
        ElseIf (LargeWarheadButton.IntersectsWith(e.Position) And CanSelectLargeWarhead) Then
            RaiseEvent ClickedWarheadButton(MissileAndWarheadTypes.Large)
        ElseIf (ExtraLargeWarheadButton.IntersectsWith(e.Position) And CanSelectExtraLargeWarhead) Then
            RaiseEvent ClickedWarheadButton(MissileAndWarheadTypes.ExtraLarge)
        ElseIf (CancelButton.IntersectsWith(e.Position)) Then
            RaiseEvent ClickedCancelButton()
        End If
    End Sub

    Public Sub HandleMouseMotion(ByVal sender As Object, ByVal e As MouseMotionEventArgs) Implements IMenu.HandleMouseMotion
        ApplyPlayerView()

        If (SmallWarheadButton.IntersectsWith(e.Position) And CanSelectSmallWarhead) Then
            SmallWarheadButton.Surface = SmallWarheadSelectedSurface
        ElseIf (MediumWarheadButton.IntersectsWith(e.Position) And CanSelectMediumWarhead) Then
            MediumWarheadButton.Surface = MediumWarheadSelectedSurface
        ElseIf (LargeWarheadButton.IntersectsWith(e.Position) And CanSelectLargeWarhead) Then
            LargeWarheadButton.Surface = LargeWarheadSelectedSurface
        ElseIf (ExtraLargeWarheadButton.IntersectsWith(e.Position) And CanSelectExtraLargeWarhead) Then
            ExtraLargeWarheadButton.Surface = ExtraLargeWarheadSelectedSurface
        ElseIf (CancelButton.IntersectsWith(e.Position)) Then
            CancelButton.Surface = CancelSelectedSurface
        End If
    End Sub

    Public Sub ShowMenuForPlayer(ByRef player As Player) Implements IMenu.ShowMenuForPlayer
        If (player.CanFireSmallWarhead()) Then
            CanSelectSmallWarhead = True
        Else
            CanSelectSmallWarhead = False
        End If
        If (player.CanFireMediumWarhead()) Then
            CanSelectMediumWarhead = True
        Else
            CanSelectMediumWarhead = False
        End If
        If (player.CanFireLargeWarhead()) Then
            CanSelectLargeWarhead = True
        Else
            CanSelectLargeWarhead = False
        End If
        If (player.CanFireExtraLargeWarhead()) Then
            CanSelectExtraLargeWarhead = True
        Else
            CanSelectExtraLargeWarhead = False
        End If

        HandleMouseMotion(Me, New MouseMotionEventArgs(False, MouseButton.None, CShort(Mouse.MousePosition.X), CShort(Mouse.MousePosition.Y), CShort(Mouse.MousePositionChange.X), CShort(Mouse.MousePositionChange.Y)))
    End Sub

    Private Sub ApplyPlayerView()
        CancelButton.Surface = CancelAvailableSurface

        If (CanSelectSmallWarhead) Then
            SmallWarheadButton.Surface = SmallWarheadAvailableSurface
        Else
            SmallWarheadButton.Surface = SmallWarheadUnavailableSurface
        End If

        If (CanSelectMediumWarhead) Then
            MediumWarheadButton.Surface = MediumWarheadAvailableSurface
        Else
            MediumWarheadButton.Surface = MediumWarheadUnavailableSurface
        End If

        If (CanSelectLargeWarhead) Then
            LargeWarheadButton.Surface = LargeWarheadAvailableSurface
        Else
            LargeWarheadButton.Surface = LargeWarheadUnavailableSurface
        End If

        If (CanSelectExtraLargeWarhead) Then
            ExtraLargeWarheadButton.Surface = ExtraLargeWarheadAvailableSurface
        Else
            ExtraLargeWarheadButton.Surface = ExtraLargeWarheadUnavailableSurface
        End If
    End Sub

    Public Sub Dispose() Implements IMenu.Dispose
        SmallWarheadAvailableSurface.Dispose()
        SmallWarheadUnavailableSurface.Dispose()
        SmallWarheadSelectedSurface.Dispose()
        MediumWarheadAvailableSurface.Dispose()
        MediumWarheadUnavailableSurface.Dispose()
        MediumWarheadSelectedSurface.Dispose()
        LargeWarheadAvailableSurface.Dispose()
        LargeWarheadUnavailableSurface.Dispose()
        LargeWarheadSelectedSurface.Dispose()
        ExtraLargeWarheadAvailableSurface.Dispose()
        ExtraLargeWarheadUnavailableSurface.Dispose()
        ExtraLargeWarheadSelectedSurface.Dispose()
        CancelAvailableSurface.Dispose()
        CancelSelectedSurface.Dispose()
        BlankSurface.Dispose()
    End Sub
End Class
